<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>模板</title>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="test1"></div>
    <div id="test2"></div>
    <div id="test3"></div>

    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom，用于支持react操作DOM -->
    <script
      type="text/javascript"
      src="../js/react-dom.development.js"
    ></script>
    <!-- 引入babel，用于将jsx转换成js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 引入属性校验js -->
    <script src="../js/prop-types.js" type="text/javascript"></script>
    <script type="text/babel">
      //1.创建组件
      class Person extends React.Component {
        state = {
          name: "名字",
        };

        render() {
          console.log(this);
          //props是只读的,不可以修改
          const { name, birth, age } = this.props;
          // this.props.name = "lucy";
          return (
            <ul>
              <li>名字:{name}</li>
              <li>年龄:{age + 1}</li>
              <li>出生日期:{birth}</li>
            </ul>
          );
        }
      }

      //对标签属性进行类型、必要性的限制
      Person.propTypes = {
        name: PropTypes.string.isRequired, //限制name必传,并且是字符串
        // birth: PropTypes.string, //限制sex非必传,且为字符串
        age: PropTypes.number, //限制age非必传,且为数字
        speak: PropTypes.func, //限制speak非必传,且为函数
      };

      //指定默认标签属性值
      Person.defaultProps = {
        // age: 22, //设置年龄的默认值为22
        // birth: "生日啦",
        name: "lucy",
      };

      ReactDOM.render(
        <Person age={20} speak={speak} />,
        document.querySelector("#test1")
      );
      const json = {
        name: "test",
        age: 1111,
      };

      ReactDOM.render(<Person {...json} />, document.querySelector("#test2"));

      function speak() {
        console.log("speakllllllllllllllllllll");
      }
    </script>
  </body>
</html>
